﻿// -----------------------------------------------------------------------
// <copyright file="PixelDouble.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ImagePackage.Model
{
    using System;

    /// <summary>
    /// Holds Red, Green, Blue double values for calculation
    /// </summary>
    public struct PixelDouble
    {
        /// <summary>
        /// double red component
        /// </summary>
        private double red;

        /// <summary>
        /// double green component
        /// </summary>
        private double green;

        /// <summary>
        /// double blue component
        /// </summary>
        private double blue;

        /// <summary>
        /// Initializes a new instance of the <see cref="PixelDouble"/> struct.
        /// </summary>
        /// <param name="red">The red.</param>
        /// <param name="green">The green.</param>
        /// <param name="blue">The blue.</param>
        public PixelDouble(double red, double green, double blue)
        {
            this.red = red;
            this.green = green;
            this.blue = blue;
        }

        /// <summary>
        /// Gets or sets the red.
        /// </summary>
        /// <value>
        /// The red.
        /// </value>
        public double Red
        {
            get { return this.red; }
            set { this.red = value; }
        }

        /// <summary>
        /// Gets or sets the green.
        /// </summary>
        /// <value>
        /// The green.
        /// </value>
        public double Green
        {
            get { return this.green; }
            set { this.green = value; }
        }

        /// <summary>
        /// Gets or sets the blue.
        /// </summary>
        /// <value>
        /// The blue.
        /// </value>
        public double Blue
        {
            get { return this.blue; }
            set { this.blue = value; }
        }

        /// <summary>
        /// Implements the plus operation.
        /// </summary>
        /// <param name="pixel1">The first pixel.</param>
        /// <param name="pixel2">The second pixel.</param>
        /// <returns>
        /// The result of the operator.
        /// </returns>
        public static PixelDouble operator +(PixelDouble pixel1, PixelDouble pixel2)
        {
            PixelDouble result = new PixelDouble(pixel1.red + pixel2.red, pixel1.green + pixel2.green, pixel1.blue + pixel2.blue);

            return result;
        }

        /// <summary>
        /// Implements the minus operation.
        /// </summary>
        /// <param name="pixel1">The first pixel.</param>
        /// <param name="pixel2">The second pixel.</param>
        /// <returns>
        /// The result of the operator.
        /// </returns>
        public static PixelDouble operator -(PixelDouble pixel1, PixelDouble pixel2)
        {
            PixelDouble result = new PixelDouble(pixel1.red - pixel2.red, pixel1.green - pixel2.green, pixel1.blue - pixel2.blue);

            return result;
        }

        /// <summary>
        /// Implements the multiply operation.
        /// </summary>
        /// <param name="pixel1">The first pixel.</param>
        /// <param name="pixel2">The second pixel.</param>
        /// <returns>
        /// The result of the operator.
        /// </returns>
        public static PixelDouble operator *(PixelDouble pixel1, PixelDouble pixel2)
        {
            PixelDouble result = new PixelDouble(pixel1.red * pixel2.red, pixel1.green * pixel2.green, pixel1.blue * pixel2.blue);

            return result;
        }

        /// <summary>
        /// Implements the divide operation.
        /// </summary>
        /// <param name="pixel1">The first pixel.</param>
        /// <param name="pixel2">The second pixel.</param>
        /// <returns>
        /// The result of the operator.
        /// </returns>
        public static PixelDouble operator /(PixelDouble pixel1, PixelDouble pixel2)
        {
            PixelDouble result = new PixelDouble(pixel1.red / pixel2.red, pixel1.green / pixel2.green, pixel1.blue / pixel2.blue);

            return result;
        }

        /// <summary>
        /// Cuts Off the double pixel value.
        /// </summary>
        /// <returns>Byte Pixel</returns>
        public Pixel CutOff()
        {
            byte red = this.red <= 255 ? this.red < 0 ? (byte)0 : (byte)this.red : (byte)255;
            byte green = this.green <= 255 ? this.green < 0 ? (byte)0 : (byte)this.green : (byte)255;
            byte blue = this.blue <= 255 ? this.blue < 0 ? (byte)0 : (byte)this.blue : (byte)255;

            return new Pixel(red, green, blue);
        }

        /// <summary>
        /// Calculates the absolute of the double pixel value.
        /// </summary>
        /// <returns>Byte Pixel</returns>
        public PixelDouble Absolute()
        {
            return new PixelDouble(Math.Abs(this.red), Math.Abs(this.green), Math.Abs(this.blue));
        }
    }
}
